//
//  UIAlertView+ShaneZhang.m
//  BlockDemoProject
//
//  Created by shanezhang on 14/12/22.
//  Copyright (c) 2014年 ShaneZhang. All rights reserved.
//

#import "UIAlertView+ShaneZhang.h"
#import <objc/runtime.h>

@implementation UIAlertView (ShaneZhang)
static char key;
- (void)showAlertViewWithCompleteBlock:(CompleteBlock)block
{
    if (block)
    {
        objc_removeAssociatedObjects(self);
        objc_setAssociatedObject(self, &key, block, OBJC_ASSOCIATION_COPY);
        self.delegate = self;
    }
    
    [self show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    ///获取关联的对象，通过关键字。
    CompleteBlock block = objc_getAssociatedObject(self, &key);
    if (block) 
    {
        ///block传值
        block(buttonIndex);
    }
}


/**
 OC中的关联就是在已有类的基础上添加对象参数。来扩展原有的类，需要引入#import <objc/runtime.h>头文件。关联是基于一个key来区分不同的关联。
 常用函数: objc_setAssociatedObject     设置关联
 objc_getAssociatedObject     获取关联
 objc_removeAssociatedObjects 移除关联
 */
@end
